package com.ckzp.core.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 51594 on 2022/7/27.
 */
public class DateUtil {


    /**
     * 把字符串转换成时间，格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param
     * @return
     */
    public static Date getDate() {
        return new Date();
    }

    /**
     * 把字符串转换成时间，格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static Date getDate(String dateStr) {
        Date temp1 = null;
        if (dateStr == null) return new Date();
        if (dateStr.equals("")) return new Date();
        SimpleDateFormat formatter = null;
        try {
            if (dateStr.indexOf(" ") != -1) {
                String[] aa = StringUtil.split(dateStr, ":");
                if (aa.length == 3)
                    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                else if (aa.length == 2)
                    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                else
                    formatter = new SimpleDateFormat("yyyy-MM-dd HH");
            } else if (dateStr.length() == 10) {
                formatter = new SimpleDateFormat("yyyy-MM-dd");
            } else if (dateStr.length() == 8) {
                formatter = new SimpleDateFormat("yyyyMMdd");
            } else if (dateStr.length() == 7) {
                formatter = new SimpleDateFormat("yyyy-MM");
            } else if (dateStr.length() == 4) {
                formatter = new SimpleDateFormat("yyyy");
            }
            temp1 = formatter.parse(dateStr);
        } catch (Exception e) {
            return null;
            //e.printStackTrace();
        }
        return temp1;
    }


    /**
     * 把字符串转换成时间，格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static Date getDate(String dateStr, String props) {
        Date temp1 = null;
        if (dateStr == null) return new Date();
        if (dateStr.equals("")) return new Date();
        SimpleDateFormat formatter = null;
        try {
            formatter = new SimpleDateFormat(props);
            temp1 = formatter.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
        return temp1;
    }


    /**
     * @return
     */
    public static int getYYYYMMDD(Date date) {
        if (date == null) return 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMdd");
        return StringUtil.getInt(dateFm.format(date), 0);
    }


    /**
     * @return
     */
    public static int getYYYYMMDDHH(Date date) {
        if (date == null) return 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMddHH");
        return StringUtil.getInt(dateFm.format(date), 0);
    }

    /**
     * @return
     */
    public static int getYYYYMMDD() {
        return getYYYYMMDD(new Date());
    }

    /**
     * @return
     */
    public static int getYYYYMM(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.setTime(date);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMM");
        return StringUtil.getInt(dateFm.format(date), 0);
    }


    /**
     * @return
     */
    public static String getCnYyyyMm(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.setTime(date);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy年MM月");
        return (dateFm.format(date));
    }

    /**
     * @return
     */
    public static String getCnWeek(Date date) {
        if (date == null) return "";
        String[] week = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar cal = Calendar.getInstance();
        // cal.add(Calendar.MONTH, 0);
        cal.setTime(date);
        return (week[cal.get(Calendar.DAY_OF_WEEK) - 1]);
    }

    /**
     * @return
     */
    public static String getCnMmDd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.setTime(date);
        SimpleDateFormat dateFm = new SimpleDateFormat("MM月dd日");
        return (dateFm.format(date));
    }

    /**
     * 根据传入的日期转换成字符形式的日期
     *
     * @param date 日期型
     * @return 如：2005-12-25 08:25:36
     */
    public static String getDateTime(java.util.Date date) {
        if (date == null) return "";
        SimpleDateFormat formatter =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String NDate = formatter.format(date);
        return NDate;
    }


    /**
     * 根据传入的日期转换成字符形式的日期
     *
     * @param date 日期型
     * @return 如：2005-12-25
     */
    public static String getDateStr(java.util.Date date) {
        if (date == null) return "";
        SimpleDateFormat formatter =
                new SimpleDateFormat("yyyy-MM-dd");
        String NDate = formatter.format(date);
        return NDate;
    }


    /**
     * 根据传入的日期转换成字符形式的日期
     *
     * @return 如：2005-12-25 08:25:36
     */
    public static String getDateTime() {

        return getDateTime(new Date());
    }

    /**
     * 把字符串转换成时间，格式为：yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static Date getTime(String dateStr) {
        Date temp1 = null;
        if (dateStr == null) return new Date();
        if (dateStr.equals("")) return new Date();
        SimpleDateFormat formatter = null;
        try {
            if (dateStr.length() == 8) {
                formatter = new SimpleDateFormat("HH:mm:ss");
            } else if (dateStr.length() == 5) {
                formatter = new SimpleDateFormat("HH:mm");
            } else if (dateStr.length() == 2) {
                formatter = new SimpleDateFormat("HH");
            }
            temp1 = formatter.parse(dateStr);
        } catch (Exception e) {
            return null;
            //e.printStackTrace();
        }
        return temp1;
    }

    /**
     * 取得当前日期的Long型
     * 如1212452121222
     *
     * @return
     */
    public static long getTime() {
        return (new java.util.Date()).getTime();
    }


    /**
     * @return
     */
    public static long getYYYYMMHHMMSS() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        Date date = cal.getTime();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMddHHmmss");
        return StringUtil.getLong(dateFm.format(date), 0);
    }


    /**
     * 日期格式化成日期年月，
     *
     * @return 200512
     */
    public static String getDateYYYYMM() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        String NDate = formatter.format(new Date());
        return NDate;
    }


    /**
     * 日期格式化成日期时分，不取秒，
     *
     * @param date
     * @return 2005-12-25 12:25
     */
    public static String getDateHF(java.util.Date date) {
        if (date == null) return "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String NDate = formatter.format(date);
        return NDate;
    }

    /**
     * 添
     *
     * @param olddate
     * @param addLong
     * @return
     */
    public static Date getAddDate(Date olddate, long addLong) {
        long temp = olddate.getTime();
        temp += addLong;
        return new Date(temp);
    }

    /**
     * 添
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 计算中文时间差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String getDateBetween(Date date1, Date date2) {
        StringBuffer sb = new StringBuffer();
        long times = date1.getTime() - date2.getTime();
        if (times < 0) times = times * -1;
        int year = (int) (times / (365 * 24 * 60 * 60 * 1000L));
        if (year > 0) {
            times = (times % (365 * 24 * 60 * 60 * 1000L));
            sb.append(year + "年");
        }
        int days = (int) (times / (24 * 60 * 60 * 1000L));
        if (days > 0) {
            times = (times % (24 * 60 * 60 * 1000L));
            sb.append(days + "天");
        }
        int hour = (int) (times / (60 * 60 * 1000L));
        if (hour > 0) {
            times = (times % (60 * 60 * 1000L));
            sb.append(hour + "时");
        }
        int minute = (int) (times / (60 * 1000L));
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (sb.toString().length() == 0) sb.append("0分");
        return sb.toString();
    }

    public static void main(String[] arg){
        System.out.println(getYYYYMMHHMMSS());
    }
}
